package com.example.docking.service.impl;

import com.example.docking.dao.jindie.ResponseData;
import com.example.docking.dao.jindie.response.SecretData;
import com.example.docking.service.KingdeeApiService;
import com.example.docking.util.Constants;
import com.example.docking.util.EncryptionUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.smecloud.apigw.client.ApigwClient;
import com.smecloud.apigw.constant.HttpMethod;
import com.smecloud.apigw.model.ApiRequest;
import com.smecloud.apigw.model.ApiResult;
import com.smecloud.apigw.model.ApigwConfig;

import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class KingdeeApiServiceImpl implements KingdeeApiService {
    // 创建 Gson 对象
    Gson gson = new Gson();

    @Override
    public ApiResult xApiSignature() throws Exception {
        KingdeeApiService kingdeeApiService = new KingdeeApiServiceImpl();
        ApiRequest request = new ApiRequest(HttpMethod.GET, Constants.HOST, Constants.TOKENPATH);
        ApigwClient apigwClient = kingdeeApiService.apigwClient();
        String appSecret = kingdeeApiService.appSecret(apigwClient);
        String appSign = EncryptionUtil.generateAppSignature(Constants.APPKEY, appSecret);
        System.out.println("AppSecret-sign:" + appSign);
        Map<String, String> map = new HashMap<>();
        map.put("app_key", Constants.APPKEY);
        map.put("app_signature", appSign);
        request.setQuerys(map);
        request.setBodyJson(gson.toJson("").getBytes(StandardCharsets.UTF_8));
        return apigwClient.getInstance().send(request);
    }

    @Override
    public String appSecret(ApigwClient apigwClient) {
        ApiRequest request = new ApiRequest(HttpMethod.POST_FORM, Constants.HOST, Constants.GRANTPATH);
        Map<String, String> map = new HashMap<>();
        map.put("outerInstanceId", Constants.OUTERINSTANCEID);
        request.setQuerys(map);
        request.setBodyJson(gson.toJson("").getBytes(StandardCharsets.UTF_8));
        ApiResult result = apigwClient.getInstance().send(request);
        Gson gson = new Gson();
        ResponseData<SecretData> response = gson.fromJson(result.getBody(), ResponseData.class);
        System.out.println("Code: " + response.getErrcode());
        System.out.println("Message: " + response.getDescription());
        Type listType = new TypeToken<List<SecretData>>() {
        }.getType();
        List<SecretData> secretData = gson.fromJson(gson.toJsonTree(response.getData()), listType);
        String appSecret = "";
//        if (response.getData() instanceof List) {
//            List<?> dataList = (List<?>) response.getData();
//            if (dataList.stream().allMatch(SecretData.class::isInstance)) {
//                List<SecretData> secretData = (List<SecretData>) dataList;
        // 进行后续的循环操作
        for (SecretData item : secretData) {
            System.out.println("App Key: " + item.getAppKey());
            System.out.println("App Secret: " + item.getAppSecret());
            // 可根据需求打印其他属性
            appSecret = item.getAppSecret();
        }
//            } else {
//                // 处理类型不匹配的情况，例如打印日志
//                System.out.println("The data in response is not in the expected format.");
//            }
//        } else {
//            // 处理response.getData()不是List类型的情况，例如打印日志
//            System.out.println("response.getData() is not a List.");
//        }
        return appSecret;
    }

    @Override
    public ApigwClient apigwClient() {
        ApigwConfig config = new ApigwConfig();
        // 设置 client_id
        config.setClientID(Constants.CLIENT_ID);
        // 设置 client_secret
        config.setClientSecret(Constants.CLIENT_SECRET);
        ApigwClient apigwClient = ApigwClient.getInstance();
        // 初始化 API 网关客户端
        apigwClient.init(config);
        return apigwClient;
    }

    public static void main(String[] args) throws Exception {
        KingdeeApiService kingdeeApiService = new KingdeeApiServiceImpl();
        ApiResult result = kingdeeApiService.xApiSignature();
        System.out.println(result.getBody());
    }
}
